package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {
    /**
     * 新增套餐
     */
    @Autowired
    SetmealService setmealService;
    @PostMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)//删除所有以dish开头的缓存
    public R<String> save(@RequestBody SetmealDto setmealDto){
    setmealService.saveWithDish(setmealDto);
    return R.success("添加成功");
    }

    /**
     * 分页查询
     */
    @GetMapping("page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name){
        Page<SetmealDto> pageInfo=setmealService.pageSetmealDto(page,pageSize,name);
        return R.success(pageInfo);
    }
    /**
     * 删除套餐
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)//删除所有以dish开头的缓存
    public R<String> deleteByIds(Long[] ids){

        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }
    /**
     * 修改停售，启售
     */
    @PostMapping("status/{status}")
    @CacheEvict(cacheNames = "setmeal",allEntries = true)//删除所有以dish开头的缓存
    public R<String> statusByIds(@PathVariable Integer status
            ,Long[] ids){
        setmealService.lambdaUpdate()
                .set(Setmeal::getStatus,status)
                .in(Setmeal::getId,ids)
                .update();

        return R.success("状态修改成功");
    }
    /**
     * 根据id查询数据
     */
    @GetMapping("{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
       SetmealDto setmealDto= setmealService.getSetmealDtoBYId(id);
        return R.success(setmealDto);

    }
    /**
     * 修改套餐
     */
    @PutMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)//删除所有以dish开头的缓存
    public R<String> updeleteById(@RequestBody SetmealDto setmealDto){

        setmealService.getdeleteById(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 根据状态查询套餐
     */
    @GetMapping("list")
    @Cacheable(cacheNames = "setmeal",key = "#categoryId+'_'+#status",unless = "#result.data.size()==0")
    public R<List<Setmeal>> listsetmeal(Long categoryId,Integer status){
        List<Setmeal> list = setmealService.lambdaQuery()
                .eq(categoryId != null, Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, 1)
                .list();
        return R.success(list);
    }

    @Autowired
    DishService DishService;
    //根据套餐id查询菜品
    @GetMapping("dish/{setmeal_id}")
    public R<List<Dish>> dish(@PathVariable Long setmeal_id){

        List<Dish> setmealIddish=DishService.getDishBySetmealId(setmeal_id);

        return R.success(setmealIddish);
    }
}
